#!/usr/bin/env python
# encoding: utf-8
'''
@author: luohui
@license: (C) Copyright 2013-2018.
@contact: 254272115@qq.com
@software: personal
@file: businessoperate.py
@time: 2018/9/30 14:37
@desc:
'''
from operate.baseoperate import BaseOperate
from operate.log import logger
from util.longpresshome import long_press_home
from config.emtconfig import ELEMENT
from selenium.common.exceptions import NoSuchElementException


class BusinessOperate(BaseOperate):
    '''
    登录页的操作
    '''
    def login_action(self, username):

        self.prompt_box()
        self.synchronization()
        logger.info('login username: %s', username)
        self.find_element(*ELEMENT[username_type]).clear().send_keys(username)
        # 点击密码输入框，收起键盘
        logger.info('click login button')
        self.find_element(*ELEMENT[password_type]).click()
        self.find_element(*ELEMENT[login_btn]).click()
        # 取消执行质控弹框
        try:
            msg_element = self.find_element(*ELEMENT[msg_type])
            msg = msg_element.text
            self.find_element(*ELEMENT[cancel_btn]).click()
        except NoSuchElementException:
            pass
        else:
            logger.info(msg)

    def is_login(self):

        try:
            logger.info(self.current_activity())
            self.find_element(*ELEMENT[login_done])
        except NoSuchElementException as e:
            logger.info('not in login page' + e.msg)
            self.get_screen_shot_as_file('login fail')
            return False
        else:
            self.get_screen_shot_as_file('login success')
            return True

    def synchronization(self):
        # 确认是否在登录界面，执行同步操作
        done = 1
        try:
            self.find_element(*ELEMENT[password_type])
        except NoSuchElementException as e:
            logger.info('not in login page' + e.msg)
        else:
            long_press_home()
        finally:
            while done:
                try:
                    self.find_element(*ELEMENT[ward_type])
                except NoSuchElementException:
                    pass
                else:
                    done = 0
                try:
                    msg_element = self.find_element(*ELEMENT[msg_type])
                    msg = msg_element.text
                    self.find_element(*ELEMENT[cancel_btn]).click()
                except NoSuchElementException:
                    pass
                else:
                    logger.warning('synchronization failed' + msg)
                    return True
                self.implicitly_wait(1)
            logger.info('synchronization done')

    def prompt_box(self):
        # 提示窗，并尝试关闭
        try:
            prompt_ele = self.find_element(*ELEMENT[msg_type])
            msg = prompt_ele.text
            self.find_element(*ELEMENT[btn]).click()
        except NoSuchElementException:
            logger.info('未发现提示窗口')
        else:
            logger.info('弹窗提示：' + msg)
            logger.info('关闭提示窗口')
            # self.find_element(*self.login_btn).click()
            try:
                self.find_element(*ELEMENT[msg_type])
            except NoSuchElementException:
                logger.info('未发现提示窗口')
            else:
                self.prompt_box()


if __name__ == '__main__':
    with open('../config/caps.yaml', 'r', encoding='utf-8') as file:
        data = yaml.load(file)

    st = SalendroidTests(data)
    login = LoginView(st.driver)
    login.login_action('admin')
    # login.tv_msg()
    st.close()
